package edu.ccut.saturn.component.utils;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class SaturnImageUtils {
	         
	public static void changeImageSize(String srcFilePath,String savePath,int width,int hight) throws Exception {
		java.io.File file=new java.io.File(srcFilePath);
		if(file.exists()){
			BufferedImage src = null;
			FileOutputStream newimage = null;
			try{
				src = ImageIO.read(file);
				BufferedImage tag = new BufferedImage(width,hight,BufferedImage.TYPE_INT_RGB);
				tag.getGraphics().drawImage(src,0,0,width,hight,null);
				newimage=new FileOutputStream(savePath);
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
				encoder.encode(tag); 
			}catch(IIOException ee){
				ee.printStackTrace();
			}catch(Exception e){
					e.printStackTrace();
			}finally{
				newimage.close();
			}
		}
	}
}
